package com.store.app.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by Ben
 * on 2016/7/21 0021.\
 */


public class DrawableCenterTextView extends TextView {

    public DrawableCenterTextView(Context context, AttributeSet attrs,
                                  int defStyle) {
        super(context, attrs, defStyle);
    }

    public DrawableCenterTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawableCenterTextView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
            Drawable drawableTop = drawables[1];

            if (drawableTop != null) {
                float textSize = getPaint().getTextSize();
                int drawHeight = drawableTop.getIntrinsicHeight();
                int drawPadding = getCompoundDrawablePadding();
                float contentHeight = textSize + drawHeight + drawPadding;
                int topPadding = (int) (getHeight() - contentHeight);
                setPadding(0, topPadding-(int)(getPaint().getTextSize()/2) , 0, 0);
                float dy = (contentHeight - getHeight())/2;
                canvas.translate(0, dy);
            }
        }
        super.onDraw(canvas);
    }

}
